﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DiaoPaiDaYin;
using Luna小店.UC.查询;
using DevExpress.XtraGrid.Demos.util;
using Luna小店.UC.款式异常;
using Luna小店.DTO;
using Luna小店.UC.门店下单.form.订单修改;
using static Luna小店.UC.门店下单.form.Frm订单一览;
using mendian;
using Luna小店.service;

namespace Luna小店.UC.销售管理.门户UC
{
    public partial class XtraUserControl客户信息查询 : DevExpress.XtraEditors.XtraUserControl
    {
        private String SYS_STYLE_ID;
        public XtraUserControl客户信息查询()
        {
            InitializeComponent();
        }
        private void textBox手机号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.load信息(this.textBox手机号.Text.Trim());
            }
        }
        private void load信息(String mobile)
        {
            #region 客户基本信息
            String sql客户基本信息 = "SELECT\n" +
                "	CONCAT( a_customer_p.CUSTOMER_FIRST_NAME, a_customer_p.CUSTOMER_LAST_NAME ) AS `姓名`,\n" +
                "	a_customer_address_p.MOBILE AS `手机`,\n" +
                "	a_customer_address_p.CONSIGNEE AS `收货姓名`,\n" +
                "	CONCAT( a_customer_address_p.ADDRESS_PROVINCE_CD, a_customer_address_p.ADDRESS_CITY_CD, a_customer_address_p.ADDRESS_DISTRICT, a_customer_address_p.ADDRESS_DETAIL ) AS `地址` \n" +
                "FROM\n" +
                "	a_customer_address_p\n" +
                "	LEFT JOIN a_customer_p ON a_customer_address_p.CUSTOMER_ID = a_customer_p.CUSTOMER_ID \n" +
                "WHERE\n" +
                "	a_customer_address_p.MOBILE = '" + mobile + "';";
            #endregion
            this.gridControl客户基本信息.DataSource = SQLmtm.GetDataTable(sql客户基本信息);
            #region 历史订单
            String sql历史订单 = "SELECT\n" +
                "	s_style_p.SYS_STYLE_ID,\n" +
                "	o_order_p.ORDER_ID AS `ORDER_ID`,\n" +
                "	o_order_p.ORDER_NO AS `订单号`,\n" +
                "	o_order_p.CUSTOM_NAME AS `订单姓名`,\n" +
                "	s_style_p.STYLE_NAME_CN AS `款式名称`,\n" +
                "	o_order_p.REMARKS AS `备注`,\n" +
                "	o_order_p.CREATE_DATE AS `下单时间` \n" +
                "FROM\n" +
                "	o_order_p\n" +
                "	LEFT JOIN s_style_p ON o_order_p.STYLE_ID = s_style_p.SYS_STYLE_ID \n" +
                "WHERE\n" +
                "	o_order_p.CUSTOMER_ID IN ( SELECT CUSTOMER_ID FROM a_customer_address_p WHERE a_customer_address_p.MOBILE = '" + mobile + "' )";
            #endregion
            this.gridControl历史订单.DataSource = SQLmtm.GetDataTable(sql历史订单);
        }

        private void simpleButton查电话_Click(object sender, EventArgs e)
        {
            new Frm客户姓名查信息().ShowDialog();
        }

        private void gridView历史订单_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            this.SYS_STYLE_ID = FunctionHelper.GetgridViewValue(this.gridView历史订单, e.RowHandle, "SYS_STYLE_ID");
            String ORDER_ID = FunctionHelper.GetgridViewValue(this.gridView历史订单, e.RowHandle, "ORDER_ID");
            
            #region 弹出订单
            String sql = "SELECT\n" +
                "	s_style_p.STYLE_NAME_CN,\n" +
                "	s_style_p.STYLE_FIT_CD,\n" +
                "	s_style_p.STYLE_CATEGORY_CD,\n" +
                "	s_style_p.STYLE_SIZE_CD,\n" +
                "	s_style_p.STYLE_SIZE_GROUP_CD,\n" +
                "	o_order_p.ORDER_ID,\n" +
                "	o_order_p.OGNIZATION_ID,\n" +
                "	o_order_p.SHOP_ID,\n" +
                "	o_order_p.SHOP_NAME,\n" +
                "	o_order_p.ORDER_NO,\n" +
                "	o_order_p.ORDER_TYPE_CD,\n" +
                "	o_order_p.CUSTOM_ORDER_NO,\n" +
                "	o_order_p.CUSTOM_NAME,\n" +
                "	o_order_p.CUSTOM_MAKE_SHIRT,\n" +
                "	o_order_p.TRANSIT_GROUP,\n" +
                "	o_order_p.COUNTRY,\n" +
                "	o_order_p.SLEEVE_FLAG,\n" +
                "	o_order_p.CUSTOMER_ID,\n" +
                "	o_order_p.CUSTOMER_NAME,\n" +
                "	o_order_p.FACTORY_ID,\n" +
                "	o_order_p.STYLE_ID,\n" +
                "	o_order_p.REF_ORDER_ID,\n" +
                "	o_order_p.SPECIAL_ORDER,\n" +
                "	o_order_p.TRYON_ORDER,\n" +
                "	o_order_p.FIT_STYLE_SIZE,\n" +
                "	o_order_p.TAILOR_ID,\n" +
                "	o_order_p.SHIPPING_DESTINATION,\n" +
                "	o_order_p.PAYMENT_DATE,\n" +
                "	o_order_p.PAYMENT_CONFIRM_DATE,\n" +
                "	o_order_p.ORDER_ACCEPT_DATE,\n" +
                "	o_order_p.ORDER_PRO_START_DATE,\n" +
                "	o_order_p.ORDER_PRO_END_DATE,\n" +
                "	o_order_p.ORDER_PACK_DATE,\n" +
                "	o_order_p.ORDER_SHIPMENTS_DATE,\n" +
                "	o_order_p.TARGET_DATE,\n" +
                "	o_order_p.REAL_DATE,\n" +
                "	o_order_p.ORDER_STATUS_CD,\n" +
                "	ORDER_STATUS.ITEM_NAME_CN,\n" +
                "	o_order_p.ORDER_PRODUCE_STATUS_CD,\n" +
                "	o_order_p.ORDER_QC34,\n" +
                "	o_order_p.ORDER_QC35,\n" +
                "	o_order_p.ORDER_QC36,\n" +
                "	o_order_p.ORDER_QC37,\n" +
                "	o_order_p.ORDER_NUMBER,\n" +
                "	o_order_p.ORDER_MATERIAL_COST,\n" +
                "	o_order_p.ORDER_DESIGN_COST,\n" +
                "	o_order_p.ORDER_PROCESS_COST,\n" +
                "	o_order_p.ORDER_PACK_COST,\n" +
                "	o_order_p.ORDER_EXPRESS_COST,\n" +
                "	o_order_p.ORDER_SELL_ACCOUNT,\n" +
                "	o_order_p.ORDER_OTHER_COST,\n" +
                "	o_order_p.MATERIAL_SOURCE,\n" +
                "	o_order_p.URGENT_CD,\n" +
                "	o_order_p.AFTER_SALE_STATUS,\n" +
                "	o_order_p.FIT_COMPLETE_FLAG,\n" +
                "	o_order_p.REMARKS,\n" +
                "	o_order_p.DELETE_FLAG,\n" +
                "	o_order_p.VERSION,\n" +
                "	o_order_p.CREATE_DATE,\n" +
                "	o_order_p.UPDATE_DATE,\n" +
                "	o_order_p.CREATE_USER,\n" +
                "	o_order_p.UPDATE_USER,\n" +
                "	o_order_p.SEL_KBN,\n" +
                "	o_order_p.ACTUAL_PAYMENT_COST,\n" +
                "	o_order_p.PREFERENTIAL_AMOUNT_COST,\n" +
                "	o_order_p.PREFERENTIAL_PERCENTAGE,\n" +
                "	DATE_FORMAT( o_order_p.ORDER_DATE, '%Y-%m-%d' ) AS ORDER_DATE,\n" +
                "	print_flag.ITEM_NAME_CN AS PRINT_STATUS,\n" +
                "	fr.BUSINESS_NO,\n" +
                "	fc.CUSTOMER_NAME AS CUSTOMER_TRUE_NAME,\n" +
                "	customer.MOBILE,\n" +
                "	SUBSTRING_INDEX( s_style_p.STYLE_SIZE_CD, '-',- 1 ) AS SIZE, \n" +
                "a_customer_address_p.CONSIGNEE AS `收货人姓名`,\n" +
                "	CONCAT( a_customer_address_p.ADDRESS_PROVINCE_CD, a_customer_address_p.ADDRESS_CITY_CD, a_customer_address_p.ADDRESS_DISTRICT, a_customer_address_p.ADDRESS_DETAIL ) AS `收货人地址`,\n" +
                "	a_customer_address_p.MOBILE AS `收货人电话` \n" +
                "FROM\n" +
                " o_order_p 	LEFT JOIN s_style_p ON o_order_p.STYLE_ID = s_style_p.SYS_STYLE_ID\n" +
                "	LEFT JOIN ( SELECT CONCAT( ITEM_CD, '-', ITEM_VALUE ) AS ITEM_VALUE, ITEM_NAME_CN FROM a_dict_p WHERE item_cd LIKE 'ORDER_STATUS' ) ORDER_STATUS ON ORDER_STATUS.ITEM_VALUE = o_order_p.ORDER_STATUS_CD\n" +
                "	LEFT JOIN ( SELECT ITEM_NAME_CN, ITEM_VALUE FROM a_dict_p WHERE ITEM_CD = 'ORDER_PRINT_STATUS' ) print_flag ON o_order_p.PRINT_FLAG = print_flag.ITEM_VALUE\n" +
                "	LEFT JOIN f_accounting_order_r fr ON o_order_p.ORDER_ID = fr.ORDER_ID\n" +
                "	LEFT JOIN a_customer_fit_count_r fc ON fc.ID = s_style_p.CUSTOMER_COUNT_ID\n" +
                "	LEFT JOIN a_customer_p customer ON o_order_p.CUSTOMER_ID = customer.CUSTOMER_ID \n" +
                "	LEFT JOIN (SELECT * FROM a_customer_address_p WHERE DEFAULT_ADDR_FLAG='1') a_customer_address_p ON o_order_p .CUSTOMER_ID=a_customer_address_p.CUSTOMER_ID \n" +
                "WHERE\n" +
                "	 o_order_p.ORDER_ID ='" + ORDER_ID + "'";

            DataRow dataRow = SQLmtm.GetDataRow(sql);

            List<尺寸呈现dto> lst = SizeService.getDto尺寸ByOrderId(
                dataRow["ORDER_ID"].ToString()
                , dataRow["STYLE_FIT_CD"].ToString()
                , dataRow["STYLE_CATEGORY_CD"].ToString()
                , dataRow["STYLE_SIZE_CD"].ToString()
                , dataRow["STYLE_SIZE_GROUP_CD"].ToString()
                , dataRow["CUSTOMER_ID"].ToString()
                );
            DTOStyleMeasureItems dTO = new DTOStyleMeasureItems();
            dTO = FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(RestCall.httpGetMethod(URL.接口url + "/style/getMeasureValue?styleID=" + dataRow["STYLE_ID"].ToString()));

            new Frm订单预览(
                dataRow["STYLE_ID"].ToString()
                , lst
                , dataRow["ORDER_ID"].ToString()
                , dataRow["REMARKS"].ToString()
                , Enum订单类别.销售订单
                , dTO
                ).HideFor任务().ShowDialog();
            #endregion

        }
    }
}
